package com.bd.rj.frontstage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.rj.common.entity.Post;
import com.bd.rj.frontstage.mapper.PostMapper;
import com.bd.rj.frontstage.service.impl.PostServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/post")
public class PostController {
    @Resource
    PostServiceImpl postService;
    @Resource
    PostMapper postMapper;
    @Autowired
    private PostServiceImpl postServiceImpl;

    /**
     * @author: WBW
     * 查询所有岗位
     * @return
     */
    @GetMapping("/queryall")
    public Map<String, Object> QueryAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", postService.list());
        return map;
    }

    /**
     * @author: WBW
     * 模糊查询岗位
     * @param postname  岗位名称
     * @return
     */
    @GetMapping("/querybypostname")
    public Map<String ,Object> QueryById(String postname,Long uid) throws JsonProcessingException {
        postServiceImpl.postPython(postname,uid);

        Map<String ,Object> map = new HashMap<>();
        QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("postname","%" + postname + "%");
        map.put("msg", "success");
        map.put("code", 200);
        map.put("data", postService.list(queryWrapper));
        return map;
    }

    /**
     * @author: WBW
     * @desc  根据地区 类型 薪资筛选
     * @param address  地区
     * @param type  类型
     * @param salary  薪资
     * @return
     */
//    @GetMapping("/conditionselect")
//    public Map<String,Object> conditionSelect(String address,String type,String salary) {
//        Map<String,Object> map = new HashMap<>();
//        map.put("msg","success");
//        map.put("code",200);
//        map.put("data",postService.conditionSelectPost(address,type,salary));
//        return map;
//    }

    /**
     * @author: WBW
     * 分页条件查询
     * @param current
     * @param size
     * @param address
     * @param type
     * @param salary
     * @return
     */
    @GetMapping("/conditionselect")
    public Map<String,Object> conditionSelect(@RequestParam(defaultValue = "0") int current,@RequestParam(defaultValue = "100000") int size, String address, String type, String salary) {
        Map<String,Object> map = new HashMap<>();
        Page<Post> page = new Page<>(current,size);
        IPage<Post> postIPage = postService.conditionSelectPost(address,type,salary,page);
        map.put("msg","success");
        map.put("code",200);
        map.put("data",postIPage);
        return map;
    }

    /**
     * @author: WBW
     * 根据id查询岗位
     * @param id 岗位的id
     * @return
     */
    @GetMapping("/getpost")
    public Map<String,Object> getPost(Long id) {
        Map<String,Object> map = new HashMap<>();
        map.put("msg","success");
        map.put("code",200);
        map.put("data",postService.getById(id));
        return map;
    }

    /**
     * @author: WBW
     * 获取最新岗位
     * @return
     */
    @GetMapping("/getnewpost")
    public Map<String,Object> getNewPost() {
        Map<String,Object> map = new HashMap<>();
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Post::getCreatetime).last("limit 10");
        map.put("msg","success");
        map.put("code",200);
        map.put("data",postService.list(queryWrapper));
        return map;
    }

    /**
     * @author: WBW
     * 获取最热岗位
     * @return
     */
    @GetMapping("/gethotpost")
    public Map<String,Object> getHotPost() {
        Map<String,Object> map = new HashMap<>();
        map.put("msg","success");
        map.put("code",200);
        map.put("data",postService.getHotPost());
        return map;
    }

    /* *
     * @author: WBW
     * 获取地区
     * @return
     */
    @GetMapping("getaddress")
    public Map<String,Object> getAddress() {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("address");
        List list = postService.listObjs(queryWrapper);
        map.put("msg","success");
        map.put("code",200);
        map.put("data",list.stream().distinct().collect(Collectors.toList()));
        return map;
    }


    /**
     * @author: WBW
     * 获取岗位类型
     * @return
     */
    @GetMapping("gettype")
    public Map<String,Object> getType() {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("type");
        List list = postService.listObjs(queryWrapper);
        map.put("msg","success");
        map.put("code",200);
        map.put("data",list.stream().distinct().collect(Collectors.toList()));
        return map;
    }

    /**
     * @author: WBW
     * 获取薪资
     * @return
     */
    @GetMapping("getsalary")
    public Map<String,Object> getSalary() {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("salary");
        List list = postService.listObjs(queryWrapper);
        map.put("msg","success");
        map.put("code",200);
        map.put("data",list.stream().distinct().collect(Collectors.toList()));
        return map;
    }

    /**
     * @author: WBW
     * 根据用户id查询推荐岗位
     * @param uid 用户id
     * @return
     */
    @GetMapping("/getpostbyuid")
    public Map<String,Object> getPostByUid(Long uid) throws JsonProcessingException {
//        List<Post> postByUid = postService.getPostByUid(uid);
        System.out.println();
        Map<String,Object> map = new HashMap<>();
        map.put("msg","success");
        map.put("code",200);
        map.put("data",postService.getPostByUid(uid));
        return map;
    }




}
